package com.cy.model;

import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@TableBind(tableName="product_output",pkName = "id")
public class ProductOutput extends BasicModel<ProductOutput> {
    public static final ProductOutput dao = new ProductOutput();
    public static final String SELECT_ALL = "`id`,`operator_id`,`express_num`,`output_time`,`remark`";

    @Override
    public Page<ProductOutput> paginate(Integer pageNumber, Integer pageSize, String column, ProductOutput model) {
        List<Object> paras = new ArrayList<>();
        StringBuilder from = new StringBuilder("FROM (product_output po INNER JOIN account a ON po.operator_id = a.account_id) LEFT JOIN product_of_output poo ON po.id = poo.output_id");
        StringBuilder select = new StringBuilder("SELECT ").append(column);

        pageNumber = pageNumber == null || pageNumber == 0 ? 1 : ++pageNumber;
        pageSize = pageSize == null || pageSize == 0 ? 20 : pageSize;
        if (model != null) {
            int i = 0;
            String[] attrNames = model.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = model.get(attrName);
                if (attrValue != null) {
                    from.append(i++ == 0 ? " WHERE " : " AND ");
                    if("start_time".equals(attrName)) {
                        from.append(" po.`output_time` >= ?");
                    } else if("end_time".equals(attrName)) {
                        from.append(" po.`output_time` <= ?");
                    } else if("express_num".equals(attrName)) {
                        from.append(" po.`express_num` LIKE ?");
                        attrValue = "%"+attrValue+"%";
                    } else if("sn".equals(attrName)) {
                        from.append(" poo.`sn` LIKE ?");
                        attrValue = "%"+attrValue+"%";
                    } else {
                        from.append(" `" + attrName + "`=?");
                    }
                    paras.add(attrValue);
                }
            }
        }
        from.append(" GROUP BY po.id ");
        from.append(" ORDER BY po.id DESC ");
        return paras.size() > 0 ? paginate(pageNumber, pageSize, select.toString(), from.toString(), paras.toArray())
                                : paginate(pageNumber, pageSize, select.toString(), from.toString());
    }

    public ProductOutput findByExpressNum(String expressNum) {
        return findFirst("select * from product_output where express_num = ?", expressNum);
    }

    public ProductOutput setId(Long id){
        return this.set("id",id);
    }

    public Long getId(){
        return this.get("id");
    }
        
    public ProductOutput setOperatorId(Long operatorId){
        return this.set("operator_id",operatorId);
    }

    public Long getOperatorId(){
        return this.get("operator_id");
    }

    public Account getOperator(){
        return Account.dao.findById(getOperatorId());
    }
        
    public ProductOutput setExpressNum(String expressNum){
        return this.set("express_num",expressNum);
    }

    public String getExpressNum(){
        return this.getStr("express_num");
    }
        
    public ProductOutput setOutputTime(Date outputTime){
        return this.set("output_time",outputTime);
    }

    public Date getOutputTime(){
        return this.get("output_time");
    }
        
    public ProductOutput setRemark(String remark){
        return this.set("remark",remark);
    }

    public String getRemark(){
        return this.getStr("remark");
    }

}